<template>
  <cp-nav-bar title="登录" />
  <div class="login-container">
    <div class="login-box">
      <h2>用户登录</h2>
      <van-form @submit="handleLogin">
        <van-cell-group inset>
          <van-field
            v-model="form.username"
            name="username"
            label="用户名"
            placeholder="请输入用户名"
            :rules="[{ required: true, message: '请输入用户名' }]"
          />
          <van-field
            v-model="form.password"
            type="password"
            name="password"
            label="密码"
            placeholder="请输入密码"
            :rules="[{ required: true, message: '请输入密码' }]"
          />
        </van-cell-group>
        <div class="button-group">
          <van-button round block type="primary" native-type="submit">
            登录
          </van-button>
        </div>
        <van-empty v-if="loginError" :description="loginError" />
      </van-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { showFailToast } from 'vant'

const router = useRouter()
const form = reactive({
  username: '',
  password: ''
})

const loginError = ref('')

const handleLogin = () => {
  // 模拟登录逻辑
  if (form.username === 'admin' && form.password === '123456') {
    // 登录成功，跳转到首页
    router.push('/')
  } else {
    loginError.value = '用户名或密码错误'
    showFailToast('用户名或密码错误')
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

.button-group {
  margin-top: 30px;
  padding: 0 10px;
}

:deep(.van-empty) {
  padding: 0;
  margin-top: 10px;
}

:deep(.van-empty__description) {
  color: #ee0a24;
  font-size: 12px;
}
</style>
